/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jard.webshop.jsfbeans;

import jard.webshop.management.OrderManagement;
import jard.webshop.nbp.Order;
import jard.webshop.nbp.Product;
import jard.webshop.util.FinancialException;
import jard.webshop.util.ProductException;
import jard.webshop.util.Utils;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author Harteex
 */
@ManagedBean(name = "cartModel")
@SessionScoped
public class CartModel implements Cart {
    
    @ManagedProperty(value="#{userModelBean}")
    private UserModelBean user;
    @ManagedProperty(value="#{orderBackingBean}")
    private OrderBackingBean orderBean;
    private Map<Product, Integer> products;
    private int count = 0;

    /** Creates a new instance of CartModel */
    public CartModel() {
        products = new Hashtable<Product, Integer>();
    }
    
    public CartModel(Map<Product, Integer> products) {
        this.products = products;
        count = 0;
        for(Integer i: products.values()) {
            count += i;
        }
    }

    @Override
    public Map<Product, Integer> getProducts() {
        return products;
    }
    
    public List<Product> getProductsAlone() {
        return new ArrayList<Product>(products.keySet());
    }

    @Override
    public void addProduct(Product p) {
        Integer i = products.get(p);
        if (i == null) {
            products.put(p, 1);
        } else {
            i++;
            products.put(p, i);
        }
        count++;
    }

    @Override
    public void removeProductOne(Product p) {
        Integer i = products.remove(p);

        if (i != null)		
            count--;

        if (i != null && i > 1) {
            products.put(p, i - 1);
        }
    }

    @Override
    public void removeProductAll(Product p) {
        Integer itemCount = products.get(p);
        products.remove(p);
        count = count - itemCount;
    }

    public String placeOrder() {
        System.out.println("Placing order");
        if(user == null || user.isLoggedIn() == false) {
            Utils.growl("Failed", "Only logged in users can order!");
            return "";
        }
        OrderManagement om = OrderManagement.getInstance();
        try {
            Order order = om.addOrder(user.getUser(), this);
            System.out.println("Placed order");
            products = new Hashtable<Product, Integer>();
            count = 0;
            return orderBean.view(order);
        } catch (FinancialException ex) {
            Utils.growl("Failed", ex.getMessage());
            return "";
        } catch (ProductException ex) {
            Utils.growl("Failed", ex.getMessage());
            return "";
        }
    }

    public void setOrderBean(OrderBackingBean orderBean) {
        this.orderBean = orderBean;
    }

    public int getCount() {
        return count;
    }
    
    public int getCount(Product p) {
        Integer productCount = products.get(p);
        return (productCount == null ? 0 : productCount);
    }

    public UserModelBean getUser() {
        return user;
    }

    public void setUser(UserModelBean user) {
        this.user = user;
    }
}
